package com.itrui.commodity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itrui.commodity.common.CustomException;
import com.itrui.commodity.pojo.Category;
import com.itrui.commodity.pojo.Product;
import com.itrui.commodity.pojo.Setmeal;
import com.itrui.commodity.service.CategoryService;
import com.itrui.commodity.mapper.CategoryMapper;
import com.itrui.commodity.service.ProductService;
import com.itrui.commodity.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 蕊蕤的小腿肚子
* @description 针对表【category(商品及套餐分类)】的数据库操作Service实现
* @createDate 2022-08-17 19:12:17
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Autowired
    private ProductService productService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类，删除之前需要进行判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Product> ProductLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        ProductLambdaQueryWrapper.eq(Product::getCategoryId,id);
        int count1 = productService.count(ProductLambdaQueryWrapper);

        //查询当前分类是否关联了商品，如果已经关联，抛出一个业务异常
        if(count1 != 0){
            //已经关联商品，抛出一个业务异常
            throw new CustomException("当前分类下关联了商品，不能删除");
        }

        //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据分类id进行查询
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if(count2 != 0){
            //已经关联套餐，抛出一个业务异常
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }

        //正常删除分类
        super.removeById(id);
    }
}




